import Vue from 'vue'
import Router from 'vue-router'
import store from '../store/store'

import routes from './routes'

Vue.use(Router);

const router = new Router({
    routes,
    mode:'history'
})

router.beforeEach((to, from, next) => {

    // console.log(to)
    // console.log(from)
    // console.log(store.state)

    if (to.meta.requireAuth) {  // 判断该路由是否需要登录权限
        if (sessionStorage.token) {  // 通过vuex state获取当前的token是否存在
            // console.log(store.state.token)
            next();
        }
        else {
            next({
                path: '/login',
                query: {redirect: to.fullPath}  // 将跳转的路由path作为参数，登录成功后跳转到该路由
            })
        }
    }
    else {
        next();
    }
})

export default router